import config from './config' // 配置文件

let loadingToLogin = false

export default {
	send(options = {}) {
		// 拼接路径
		options.url = config.base_api_url + options.url
		// 请求方式
		options.method = options.method || 'GET'
		// 携带token
		let access_token = uni.getStorageSync('access_token')
		if (access_token != null) {
			options.header = {
				'terminal': 'wechat',
				'isMobile': 'Y',
				'Authorization': 'Bearer ' + access_token
			}
		}
		// 发起Promise请求
		return new Promise((resolve, reject) => {
			uni.request(options).then(data => {
				// const {data:res} = data
				if (data.data.code != 0) {
					uni.showToast({
						title: '请求出错,请稍后再试！',
						icon: 'none',
						duration: 3000,
					})
					reject(error)
				} else {
					if (data.statusCode == '429') {
						uni.showToast({
							title: '当前服务繁忙,请稍后再试!',
							icon: 'none',
							duration: 3000
						})
						reject(data.data)
					}
					if (data.data.code == '401') {
						// 会话已过期，请重新登录
						uni.showToast({
							title: '用户未登录或会话已过期，请重新登录',
							icon: 'none',
							duration: 3000
						})
						uni.removeStorageSync('access_token')
						if (loadingToLogin) {
							return
						} else {
							loadingToLogin = true
							setTimeout(function() {
								loadingToLogin = false
								uni.redirectTo({
									url: '/pages/login/index'
								})
							}, 1000)
						}
					} else if (data.data.code != '0') {
						uni.showToast({
							title: data.data.msg || '请求出错！',
							icon: 'none',
							duration: 3000
						})
						reject(data.data)
					} else {
						resolve(data.data)
					}
				}
			})
		})
	},
	//上传文件
	upload(options = {}) {
		// 拼接路径
		options.url = config.base_api_url + options.url
		// 请求方式
		options.method = options.method || 'GET'
		// 携带token
		let access_token = uni.getStorageSync('access_token')
		if (access_token != null) {
			options.header = {
				'isMobile': 'Y',
				'Authorization': 'Bearer ' + access_token
			}
		}
		// 发起Promise请求
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: options.url,
				header: options.header,
				filePath: options.data.filePath,
				name: options.data.fileName,
				// formData: {},
				success: (res) => {
					if (typeof(res.data) == 'string') {
						res.data = JSON.parse(res.data)
					}
					if (res.data.code === 401) {
						// 会话已过期，请重新登录
						uni.showToast({
							title: '用户未登录或会话已过期，请重新登录',
							icon: 'none',
							duration: 3000
						})
						uni.removeStorageSync('access_token')
						setTimeout(function() {
							uni.redirectTo({
								url: '/pages/login/index'
							})
						}, 1000)
					} else if (res.data.code !== 200) {
						uni.showToast({
							title: res.data.msg || '请求出错！',
							icon: 'none',
							duration: 3000
						})
						reject(res.data)
					} else {
						resolve(res.data)
					}
				},
				fail: (error) => {
					reject(error)
				}
			})
		})
	},
	get(url = '', data = {}) {
		return this.send({
			url: url,
			data: data
		})
	},
	post(url = '', data = {}) {
		return this.send({
			url: url,
			data: data,
			method: 'POST',
			Headers: {
				'content - type':'application / json',
				'cache - control':'no - cache',
				'Access - Control - Allow - Headers':'appId',
				'Access - Control - Allow - Methods':'POST,GET,OPTIONS',
				'Access - Control - Max - Age':86400,
			}
		})
	},
	uploadfile(url = '', data = {}) {
		return this.upload({
			url: url,
			data: data,
			method: 'POST'
		})
	},
	put(url = '', data = {}) {
		return this.send({
			url: url,
			data: data,
			method: 'put'
		})
	},
	delete(url = '', data = {}) {
		return this.send({
			url: url,
			data: data,
			method: 'delete'
		})
	}
}